﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyHocPhi.Model;

namespace QuanLyHocPhi.BLL
{
    partial class BizBienCheSinhVienVaoHocKy
    {
        /// <summary>
        /// Biên chế sinh viên vào một học kỳ kèm học phí/lệ phí
        /// </summary>
        /// <param name="maSinhVien"></param>
        /// <param name="maHocKy"></param>
        /// <param name="leHocPhi"></param>
        /// <param name="laHocPhi"></param>
        /// <returns></returns>
        internal static BienCheSinhVienVaoHocKy BienCheSinhVien(string maSinhVien, string maHocKy, int leHocPhi, decimal giam, bool laHocPhi = true)
        {
            using (var context = new QLHPEntities())
            {
                var bienChe = (from e in context.BienCheSinhVienVaoHocKiDbSet
                               where
                               (e.MaSinhVien == maSinhVien) &&
                               (e.MaHocKy == maHocKy)
                               select e).SingleOrDefault();
                if (bienChe == null)
                {
                    bienChe = new BienCheSinhVienVaoHocKy()
                    {
                        MaSinhVien = maSinhVien,
                        MaHocKy = maHocKy,
                        HocPhi = leHocPhi,
                        MaBienChe = Guid.NewGuid().ToString(),
                        Giam = giam,
                    };
                    context.BienCheSinhVienVaoHocKiDbSet.Add(bienChe);
                    context.SaveChanges();
                }
                else
                {
                    if (laHocPhi)
                        bienChe.HocPhi = leHocPhi;
                    else
                        bienChe.LePhi = leHocPhi;
                    bienChe.Giam = giam;
                    context.SaveChanges();
                }
                return bienChe;
            }
        }
        internal static List<BienCheSinhVienVaoHocKy> GetBienCheSinhVien(string maSinhVien, string maNamHoc)
        {
            using (var context = new QLHPEntities())
            {
                var bienChe = (from e in context.BienCheSinhVienVaoHocKiDbSet
                               where
                               (e.MaSinhVien == maSinhVien) &&
                               (e.HocKy.NamHoc.TenNamHoc == maNamHoc)
                               select e).ToList();

                return bienChe;
            }
        }
        internal static BienCheSinhVienVaoHocKy GetBienCheSinhVienTheoHocKy(string maSinhVien, string maHocKy)
        {
            using (var context = new QLHPEntities())
            {
                var bienChe = (from e in context.BienCheSinhVienVaoHocKiDbSet
                               where
                               (e.MaSinhVien == maSinhVien) &&
                               (e.MaHocKy == maHocKy)
                               select e).FirstOrDefault();

                return bienChe;
            }
        }
        internal static List<BienCheSinhVienVaoHocKy> GetBienCheSinhVien(string maSinhVien)
        {
            using (var context = new QLHPEntities())
            {
                var bienChe = (from e in context.BienCheSinhVienVaoHocKiDbSet
                               where e.MaSinhVien == maSinhVien
                               select e).OrderBy(e => e.HocKy.MaNamHoc).ThenBy(e => e.HocKy.TenHocKy).ToList();
                return bienChe;
            }
        }
        internal static bool Delete(string maBienChe)
        {
            using (var context = new QLHPEntities())
            {
                try
                {
                    var bc = (from e in context.BienCheSinhVienVaoHocKiDbSet
                              where e.MaBienChe == maBienChe
                              select e).SingleOrDefault();
                    if (bc != null)
                    {
                        context.BienCheSinhVienVaoHocKiDbSet.Remove(bc);
                        context.SaveChanges();
                        return true;
                    }
                    else
                        return false;
                }
                catch
                {
                    return false;
                }
            }
        }
    }
}
